package com.qk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.dto.BusinessDto;
import com.qk.entity.Business;
import com.qk.entity.BusinessTrackRecord;
import com.qk.entity.Customer;
import com.qk.mapper.BusinessMapper;
import com.qk.mapper.BusinessTrackRecordMapper;
import com.qk.mapper.CustomerMapper;
import com.qk.service.BusinessService;
import com.qk.utils.CurrentUserHoler;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business> implements BusinessService {
    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private BusinessTrackRecordMapper businessTrackRecordMapper;
    @Autowired
    private CustomerMapper customerMapper;

    /**
     *查询商机列表
     */
    @Override
    public PageResult<Business> listBusiness(BusinessDto businessDto) {
        Page<Business> page = businessMapper.listBusiness(new Page<Business>(businessDto.getPage(),businessDto.getPageSize()),businessDto);
        return new PageResult<Business>(page.getTotal(),page.getRecords());
    }


    /**
     *根据Id查询商机
     */
    @Override
    public Business getBusinessById(Integer id) {
        return businessMapper.selectBusinessById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void trackBusiness(Business business) {
        //更新商机基本 信息
        business.setStatus(3);//跟进中
        business.setUpdateTime(LocalDateTime.now());//跟新时间
        businessMapper.updateById(business);//根据Id更新商机表
        //添加商机跟踪记录
        BusinessTrackRecord businessTrackRecord = new BusinessTrackRecord();
        businessTrackRecord.setBusinessId(business.getId());
        businessTrackRecord.setUserId(CurrentUserHoler.getCurrentUser());//当前用户Id
        businessTrackRecord.setTrackStatus(business.getTrackStatus());
        businessTrackRecord.setKeyItems(business.getKeyItems().toString());
        businessTrackRecord.setNextTime(business.getNextTime());
        businessTrackRecord.setRecord(business.getRecord());
        businessTrackRecord.setCreateTime(LocalDateTime.now());

        businessTrackRecordMapper.insert(businessTrackRecord);
    }


    /**
     *商机转客户
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void toCustomer(Integer id) {
        //根据id查询商机并修改商机状态
        Business business = businessMapper.selectById(id);
        business.setStatus(5);
        business.setUpdateTime(LocalDateTime.now());
        businessMapper.updateById(business);

        //添加商机到客户表
        Customer customer = new Customer();
        BeanUtils.copyProperties(business,customer);
        customer.setId(null);
        customer.setBusinessId(id);
        customer.setCreateTime(LocalDateTime.now());
        customer.setUpdateTime(LocalDateTime.now());

        customerMapper.insert(customer);
    }

    /**
     * 查询公海池
     */
    @Override
    public PageResult<Business> listBusinessPool(BusinessDto businessDto) {

        LambdaQueryWrapper<Business> wrapper = new LambdaQueryWrapper<>();
        /*wrapper.eq(Business::getId , businessDto.getBusinessId())
                .like(Business::getName,businessDto.getName())
                .like(Business::getPhone,businessDto.getPhone())
                .eq(Business::getSubject,businessDto.getSubject())
                .eq(Business::getStatus,4);*/
        Page<Business> page = businessMapper.listBusinessPool(new Page<Business>(businessDto.getPage(),businessDto.getPageSize()),businessDto);
        return new PageResult<Business>(page.getTotal(),page.getRecords());
    }
}
